/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.registration.web;

import com.registration.ManagedBean;
import com.registration.core.Attendee;
import com.registration.core.MemberVerification;
import java.util.Locale;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import org.springframework.context.MessageSource;

/**
 * @date May 22, 2012
 *
 * @author ggutierrez
 */
public class ConfirmationBean extends ManagedBean {

    private Attendee attendee;
    private String message;
    protected MessageSource messageSource;

    public ConfirmationBean() {
    }

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Override
    public void reset() {
        attendee = null;
        super.reset();
    }

    @Override
    public String cancel() {
        return "success";
    }

    public String init() {
        reset();
        attendee = new Attendee();
        return "success";
    }

    @Override
    public String startup(ActionEvent e) {
        UIParameter param = (UIParameter) e.getComponent().getChildren().get(0);
        Long id = (Long) param.getValue();
        this.attendee = getManager().loadAttendee(id);
        return "success";
    }

    public String startup(Attendee attendee) {
        reset();
        Long id = attendee != null ? attendee.getId() : null;
        this.attendee = getManager().loadAttendee(id);
        if (attendee != null) {
            if (attendee.getCategory() != null && attendee.getCategory().isRequiresValidation()) {
                if (attendee.getMemberVerification() != null && attendee.getMemberVerification().equals(MemberVerification.VERIFICADO)) {
                    message = messageSource.getMessage("verification.verifiedMember", null, Locale.getDefault());
                } else if (attendee.getMemberVerification() != null && attendee.getMemberVerification().equals(MemberVerification.VERIFICADO_CON_CODIGO)) {
                    message = messageSource.getMessage("verification.requiredCode", null, Locale.getDefault());
                } else {
                    message = messageSource.getMessage("verification.required", null, Locale.getDefault());
                }
            } else {
                message = messageSource.getMessage("verification.success", null, Locale.getDefault());;
            }
        } else {
            message = "Error al procesar los datos";
        }
        return "success";
    }

    public String save() {
        return "success";
    }

    public Attendee getAttendee() {
        return attendee;
    }

    public void setAttendee(Attendee attendee) {
        this.attendee = attendee;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
